﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFWS.XHandler.Core;
using JKF.Substructure;
using JKF.Entities.Model;
using JKF.BusinessLayer.BLL;

using JKF.DataModule.SqlServerDAL;
using JKF.BusinessLayer;
using JKF.Entities;
using System.Data;
using JKF.DataModule;
namespace JKF.HandlerLayer.Sys
{
    public class SysModuleHandler : AHttpHandler
    {
        public override void ProcessRequest()
        {
            //所有错误和异常将抛出异常.正常执行程序则在方法则返回数据
            try
            {
                this.Manager(Utils.GetActionName);
            }
            catch (Exception ex)
            {
                //将错误抛到客户端(以Json格式)
                Utils.ToClient(true, ex.Message);
                //写日志
                AppLogger.Log(ex.Message, ex);
            }
        }
        
        private static string MsgObjFormat = "{{key:'{0}',message:'{1}'}}";
        private static bllSYS_MODULE sysModuleM = new bllSYS_MODULE();

        private static bllSYS_MODULE SysModuleM
        {
            get
            {
                if (sysModuleM != null) return sysModuleM;
                sysModuleM =  new bllSYS_MODULE();
                return sysModuleM;
            }
        }

        /// <summary>
        /// 取得模块列表
        /// </summary>
        public void GetModules()
        {
            //bllSYS_MODULE sysModule = new bllSYS_MODULE();
            bllSYS_MODULE sysModuleManager = new bllSYS_MODULE();
            var obj = new PagerList();
            
            int  pageCount=0;
            int  rowCount=0;

            obj.Entity = from a in sysModuleManager.GetModules(P("MODULE_NAME"), P("DATA_START"), P("DATA_END"), P("PageIndex").ToInt32(), P("PageSize").ToInt32(), ref pageCount, ref rowCount).AsEnumerable()
                         select new
                         {
                             MODULE_KEY = a.Field<string>("MODULE_KEY"),
                             MODULE_CODE = a.Field<string>("MODULE_CODE"),
                             MODULE_NAME = a.Field<string>("MODULE_NAME"),
                             MODULE_NAME_EN = a.Field<string>("MODULE_NAME_EN"),
                             URL = a.Field<string>("URL"),
                             URL_PIC_SMALL = a.Field<string>("URL_PIC_SMALL"),
                             URL_PIC_LARGE = a.Field<string>("URL_PIC_LARGE"),
                             DISPLAY_ORDER = a.Field<decimal>("DISPLAY_ORDER"),
                             REMARK = a.Field<string>("REMARK"),
                             DATE_CREATE = a.Field<string>("DATE_CREATE"),
                             DATE_UPDATE = a.Field<string>("DATE_UPDATE")
                         };

            obj.RowCount = rowCount;
            obj.PageCount = pageCount;
            ResponseJsonObject = obj;
        }

        /// <summary>
        /// 根据模块key获得模块信息
        /// </summary>
        public void GetModuleByKey()
        {
            try
            {
                var scid = P("moduleKey");
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = sysModuleM.GetModuleInfo(scid) }, Utils.GetDateFormat));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        public class SYS_MODULE
        {
            public string MODULE_CODE;
            public string MODULE_NAME;
            public string MODULE_NAME_EN;
            public string URL;
            public string URL_PIC_SMALL;
            public string URL_PIC_LARGE;
            public string DISPLAY_ORDER;
            public string MODULE_KEY;
        }

        /// <summary>
        /// 从Json对象构建SYS_MODULE
        /// </summary>
        /// <returns></returns>
        public SYS_MODULEinfo BuildSysModuleInfoByJson(SYS_MODULE sysModule)
        {
            var entity = new SYS_MODULEinfo();
            int intTmp;

            if (sysModule.MODULE_KEY.IsGuidB())
                entity.MODULE_KEY = sysModule.MODULE_KEY;

            entity.MODULE_CODE = sysModule.MODULE_CODE;
            entity.MODULE_NAME = sysModule.MODULE_NAME;
            entity.MODULE_NAME_EN = sysModule.MODULE_NAME_EN;
            entity.URL = sysModule.URL.ToString();
            entity.URL_PIC_SMALL = sysModule.URL_PIC_SMALL.ToString();
            entity.URL_PIC_LARGE = sysModule.URL_PIC_LARGE.ToString();
            if (int.TryParse(sysModule.DISPLAY_ORDER==""?sysModule.DISPLAY_ORDER:"0", out intTmp))
                entity.DISPLAY_ORDER = intTmp;
            else
                throw new Exception("显示顺序格式不正确.");
            entity.DATE_CREATE = System.DateTime.Now;
            return entity;
        }

        public void EditOrInsertSysModule()
        {
            try
            {
                var strScr = P("moduleObj");
                var sysModuleInfo = BuildSysModuleInfoByJson(JsonSerializer.Deserialize<SYS_MODULE>(strScr));
                var tid = sysModuleInfo.MODULE_KEY;
                var pk=sysModuleM.EditOrInsertSysModule(sysModuleInfo);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = tid.IsGuidB() ? "编辑成功." : "添加成功.",Data =pk }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        public void DeleteModule()
        {
            try
            {
                var moduleKey = P("moduleKey");

                sysModuleM.DeleteModule(moduleKey);
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = moduleKey }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        public void SuggestModule()
        {
            var keyWord = P("qry");
            //页数[第一页为0]
            var page = Convert.ToInt32(Request["_pageNo"]);
            Response.Write(JsonSerializer.Serialize(sysModuleM.SuggestModule(keyWord, page)));
        }
    }
}
